import bus from '@/utils/bus'
let Audio

interface state {
  video: string
}
const state: state = {
  video: ''
}

const mutations = {
  VIDEO_PALY(state, payload) {
    console.log(payload)

    if (Audio) {
      Audio.destroy()
      Audio = null
    }

    Audio = uni.createInnerAudioContext()

    Audio.autoplay = false
    Audio.src = payload

    Audio.onPlay(() => {
      console.log('开始播放')
    })

    Audio.onError((res) => {
      console.log('播放失败', res)
    })

    Audio.onEnded(() => {
      console.log('播放结束')
      Audio.destroy()
      Audio = null
      state.video = ''
    })

    state.video = payload

    Audio.play()
  },
  VIDEO_STOP(state) {
    if (Audio) {
      Audio.destroy()
      Audio = null
    }
    state.video = ''
  }
}

const actions = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
